import request from '../instance'
import { API_ROUTES } from '../config'

export interface LoginParams {
  email: string
  password: string
}

export interface RegisterParams {
  email: string
  password: string
  name: string
  familyId: string
}

export interface UserResponse {
  id: string
  email: string
  name: string
  familyId?: string
  createdAt: string
}

export const userApi = {
  // 登录
  login: (params: LoginParams) => {
    return request.post<any, { access_token: string; user: UserResponse }>(
      API_ROUTES.LOGIN,
      params
    )
  },

  // 注册
  register: (params: RegisterParams) => {
    return request.post<any, UserResponse>(API_ROUTES.REGISTER, params)
  },

  // 加入家庭
  joinFamily: (userId: string, familyId: string) => {
    return request.put<any, UserResponse>(API_ROUTES.JOIN_FAMILY(userId), {
      familyId
    })
  },

  // 更新用户信息
  updateUser: (userId: string, data: Partial<UserResponse>) => {
    return request.put<any, UserResponse>(API_ROUTES.UPDATE_USER(userId), data)
  }
}